package system;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import interfaces.IDisplayObserver;
import interfaces.IRemoteNode;
import it.unibo.baseEnv.basicFrame.EnvFrame;
import it.unibo.is.interfaces.IBasicEnvAwt;
import device.RemoteNode;
import display.DisplayGuiOdometerObserver;

/**
 * @author Stefano&Davide
 * @version 1.0
 */

public class RemoteOdometer2012Main {
	
	protected IDisplayObserver ckmDisplay = null;
	protected IBasicEnvAwt env = null;
	protected IRemoteNode odometerNode=null;
	protected IRemoteNode localNode=null;
	
	public RemoteOdometer2012Main(String remoteIp,int remotePort,String localIp,int localPort){
		odometerNode=new RemoteNode(remoteIp, remotePort,IRemoteNode.UDP);
		localNode=new RemoteNode(localIp, localPort,IRemoteNode.UDP);
	}
	
	public RemoteOdometer2012Main(IRemoteNode nodeRem,IRemoteNode nodeLoc){
		odometerNode=nodeRem;
		localNode=nodeLoc;
	}
	
	public RemoteOdometer2012Main() { }
	
	protected void configureTheSystem(){
		env =new EnvFrame("RemoteContaKm2012");
		env.init();
		ckmDisplay=new DisplayGuiOdometerObserver(env,localNode);
		ckmDisplay.registryToGauge(odometerNode);
		ckmDisplay.startToDisplay();
	}
	
	//ONLY FOR UDP
	public void readRemoteNodesFromFile() {
		String path = "config";
		String ipRem,ipLoc,portRem,portLoc;
		try {
			File file = new File(path);
			FileReader fr = new FileReader(file);
			BufferedReader br = new BufferedReader(fr);
			ipRem=br.readLine();
			portRem=br.readLine();
			this.odometerNode=new RemoteNode(ipRem,Integer.parseInt(portRem),IRemoteNode.UDP);
			ipLoc=br.readLine();
			portLoc=br.readLine();
			this.localNode=new RemoteNode(ipLoc,Integer.parseInt(portLoc),IRemoteNode.UDP);
			br.close();
		}
		catch(IOException e) { 
			e.printStackTrace();
		}	
	}
	
	public static void main(String[] args) throws Exception{
		RemoteOdometer2012Main program=new RemoteOdometer2012Main();
		program.readRemoteNodesFromFile();
		program.configureTheSystem();
	}
	
}
